//
//  FreshHallViewController.m
//  Youhuiju
//
//  Created by 余尚飞 on 16/8/10.
//  Copyright © 2016年 yutenys. All rights reserved.
//

#import "FreshHallViewController.h"
//跳转界面
#import "GoodsDetailViewController.h"//商品详情

//自定义视图
#import "FreshHallTopView.h"//顶上视图
#import "FreshHallSectionView.h"//横向滑动菜单


//自定义cell
#import "AdImgTableViewCell.h"//顶上大图
#import "SpecialProductGoodsTableViewCell.h"//商品cell



@interface FreshHallViewController ()<UITableViewDelegate,UITableViewDataSource,SectionClickedDelegate,table_collectCellClicked,addShopCartDelegate>
@property(nonatomic,strong)FreshHallTopView *topView;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property(nonatomic,strong)FreshHallSectionView *sectionView;//sectionView

@property(nonatomic,strong)UIButton *toTopButton;//回顶部

@property(nonatomic,assign)NSInteger flag;//判断下拉状态
@property(nonatomic,assign)NSInteger sectionHeight;//section的高度

@property(nonatomic,assign)NSInteger numb;

@end

@implementation FreshHallViewController

static NSString *AdImgTableViewCellIden = @"AdImgTableViewCellIden";

static NSString *SpecialProductGoodsTableViewCellIdenti = @"SpecialProductGoodsTableViewCellIdenti";


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self load_UI];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"AdImgTableViewCell" bundle:nil] forCellReuseIdentifier:AdImgTableViewCellIden];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"SpecialProductGoodsTableViewCell" bundle:nil] forCellReuseIdentifier:SpecialProductGoodsTableViewCellIdenti];
    
    
    
    // Do any additional setup after loading the view from its nib.
}

#pragma mark ----- load_UI
-(void)load_UI
{
    self.numb = 0;
    self.flag = 0;
    self.sectionHeight = 88 * FitH;
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self topView];
    
    _toTopButton = [UIButton buttonWithType:UIButtonTypeSystem];
    _toTopButton.backgroundColor = [UIColor clearColor];
    [_toTopButton setBackgroundImage:LoadImage(@"toTop") forState:UIControlStateNormal];
    [_toTopButton addTarget:self action:@selector(toTopAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_toTopButton];
    [_toTopButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view.mas_right).offset(-20 * FitW);
        make.bottom.equalTo(self.view.mas_bottom).offset(-100 * FitH);
        make.size.mas_equalTo(CGSizeMake(40 * FitW, 40 * FitH));
    }];
    _toTopButton.alpha = 0;
}


#pragma mark ------ tableView delegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0)
    {
        return 1;
    }
    return 7;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 1)
    {
//        return 101 * FitH;
      return self.sectionHeight;
    }
    return 0;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.section)
    {
        case 0:
        {
            return 166 * FitH;
        }
            break;
        case 1:
        {
            return (68 + 222 * 10 / 2 + 17 * (10 / 2 -1)) * FitH;
        }
            break;
        default:
            return 0;
    }
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    WeakObj(self);
    if (section == 1)
    {
        weakself.sectionView = [FreshHallSectionView getInstance];
        weakself.sectionView.backgroundColor = [UIColor whiteColor];
        weakself.sectionView.sectionDelegate = self;
        weakself.sectionView.buttonClickedHandler = ^(NSInteger buttonTag,NSInteger itemNumber){
            if (buttonTag == 0)
            {
                DLog(@"牛肉");
                
                [weakself.tableView setContentOffset:CGPointMake(0, 0)];
            }
            else if(buttonTag == 1)
            {
                DLog(@"羊肉");
                [weakself.tableView setContentOffset:CGPointMake(0, 0)];
            }
            else
            {
                if (weakself.flag == 0)
                {
                    DLog(@"弹出");
                    [weakself.sectionView.flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
                    weakself.flag = 1;
                    weakself.sectionHeight = (88 + 36 * (itemNumber / 6 - 1)) * FitH;
//                                    NSIndexSet *sectionSet = [NSIndexSet indexSetWithIndex:1];
//                                    [weakself.tableView reloadSections:sectionSet withRowAnimation:UITableViewRowAnimationNone];
                    //                [weakself.sectionView.collecitonView reloadData];
//                                    [weakself.sectionView.collecitonView reloadSections:[NSIndexSet indexSetWithIndex:0]];
                    [weakself.sectionView.pullDownBtn setBackgroundImage:LoadImage(@"heishang") forState:UIControlStateNormal];

                    [weakself.tableView reloadData];
                    
                }
                else
                {
                    DLog(@"收回");
                    [weakself.sectionView.flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
                    weakself.flag = 0;
                    weakself.sectionHeight = 88 * FitH;
//                                    NSIndexSet *sectionSet = [NSIndexSet indexSetWithIndex:1];
//                                    [weakself.tableView reloadSections:sectionSet withRowAnimation:UITableViewRowAnimationNone];
                    //                [weakself.sectionView.collecitonView reloadSections:[NSIndexSet indexSetWithIndex:0]];
                    [weakself.sectionView.pullDownBtn setBackgroundImage:LoadImage(@"heixia") forState:UIControlStateNormal];

                    [weakself.tableView reloadData];
                }
            };

        };
        
        return weakself.sectionView;
    }
    return nil;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.section)
    {
        case 0:
        {
            AdImgTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:AdImgTableViewCellIden forIndexPath:indexPath];
            return cell;
        }
            break;
        case 1:
        {
            SpecialProductGoodsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SpecialProductGoodsTableViewCellIdenti forIndexPath:indexPath];
            _sectionView.num = indexPath.row ;

            cell.table_collectCellDelegate = self;
            cell.addShopCartDelegate = self;
            return cell;
        }
            break;
        default:
            return nil;
    }
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    _sectionView.num = indexPath.row ;
    DLog(@"willDisplayCell:%zd",indexPath.row);
}


#pragma mark -------- private delegate
//
-(void)changeTableViewContentOffset:(NSInteger)itemRow
{
//        _sectionView.num = itemRow;
//    if (itemRow == 0)
//    {
//        self.tableView.contentOffset = CGPointMake(0 * FitW,0);
//    }
//    else
//    {
//        
//        NSIndexPath
//        *scrollIndexPath = [NSIndexPath indexPathForRow:itemRow  inSection:1];
//        
//        DLog(@"%zd",self.numb);
//        
//        if (itemRow > self.numb)//向右点击
//        {
//            [self.tableView scrollToRowAtIndexPath:scrollIndexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
//            self.numb = itemRow;
//        }else//向左点击
//        {
//            [self.tableView scrollToRowAtIndexPath:scrollIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
//            self.numb = itemRow;
//            
//        }
//        
//        
//    }
    _sectionView.num = itemRow;
    
    if (itemRow == 0)
    {
        self.tableView.contentOffset = CGPointMake(0 * FitW,0);
    }
    else
    {
        
        NSIndexPath
        *scrollIndexPath = [NSIndexPath indexPathForRow:itemRow  inSection:1];
        
        DLog(@"%zd",self.numb);
        
        if (itemRow >= self.numb)//向右点击
        {
            [self.tableView scrollToRowAtIndexPath:scrollIndexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
            self.numb = itemRow;
        }else//向左点击
        {
            [self.tableView scrollToRowAtIndexPath:scrollIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
            self.numb = itemRow;
            
        }
        
        
    }

    
}


//cell点击代理方法
-(void)clickedJump
{
    GoodsDetailViewController *gvc = [[GoodsDetailViewController alloc]initWithNibName:@"GoodsDetailViewController" bundle:nil];
    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:gvc animated:YES];
}

-(void)addShopCartAction:(NSString *)model
{
    DLog(@"这是加入购物车button-----------%@",model);
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"已添加购物车" delegate:self cancelButtonTitle:@"知道了" otherButtonTitles:nil, nil];
    [alert show];
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    WeakObj(self);
    
    if (scrollView.contentOffset.y > 700 * FitH)
    {
        [UIView animateWithDuration:1 animations:^{
            weakself.toTopButton.alpha = 1;
        }];
    }
    else
    {
        [UIView animateWithDuration:1 animations:^{
            weakself.toTopButton.alpha = 0;
        }];
    }
}


#pragma mark ------ private Method
-(void)toTopAction:(UIButton *)sender
{
    DLog(@"回顶部");
    [self.tableView setContentOffset:CGPointMake(0, 0) animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark ------ lazy loading
-(FreshHallTopView *)topView
{
    WeakObj(self);
    if (!_topView)
    {
        _topView = [[FreshHallTopView alloc]init];
        _topView.titleString = @"生鲜会馆";
        //        _topView.backgroundColor = [UIColor redColor];
        [self.view addSubview:_topView];
        
        _topView.backButtonClickedHandler = ^{
            
            [weakself.navigationController popViewControllerAnimated:YES];
        };
        
        [_topView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.view.mas_left);
            make.top.equalTo(self.view.mas_top).offset(20);
            make.right.equalTo(self.view.mas_right);
            make.height.mas_offset(44);
        }];
    }
    return _topView;
}

@end
